<?php

require "include/template_manager.inc.php";
if (substr($_REQUEST['titolo'], -1) == '/') {

    $_REQUEST['titolo'] = substr_replace($_REQUEST['titolo'], "", -1);
}
$query = "SELECT `r`.`Id` AS `id`, `r`.`ingredienti` AS `ingredienti`, `r`.`ingredienti_en` AS `ingredienti_en`, `r`.`titolo` AS `titolo`, `r`.`titolo_en` AS `titolo_en`,`r`.`Tempo` AS `tempo`,`r`.`Difficolta` AS `difficolta`,"
        . "`i`.`meta_description` as `meta_description`,`i`.`meta_description_en` as `meta_description_en`,`i`.`meta_keywords` as `meta_keywords`,`i`.`meta_keywords_en` as `meta_keywords_en`,"
        . "`r`.`Data` as `data`,`i`.`uri` as `uri`, concat(`p`.`nome`,' ',`p`.`cognome`) AS `autore`,"
        . "`p`.`titolo` AS `prenome`,`i`.`descrizione` AS `descrizione`,`i`.`descrizione_en` AS `descrizione_en`,`r`.`persone` as `persone` "
        . "FROM ((`ricette` `r` join `immagine` `i`) join `professori` `p`) "
        . "WHERE (`r`.`immagine` = `i`.`id`) and (`r`.`autore` = `p`.`id`) and ";
if (getLanguage() == 'it') {
    $query .= "(`r`.`titolo` like '" . $_REQUEST['titolo'] . "')";
} else {
    $query .= "(`r`.`titolo_en` like '" . $_REQUEST['titolo'] . "')";
}
if (($data = DB_Manager::getInstance()->query($query)) && ($row = $data->fetch_array(MYSQLI_ASSOC))) {
    require_once 'include/language_manager.inc.php';
    Language_Manager::getInstance();
    $prefix = "/";
    $labelRecipe = "ricetta";
    if (getLanguage() == 'it') {
        $meta["title"] = $row['titolo'] . "|Ricetta scuola di cucina";
        $meta["description"] = $row['meta_description'];
        $meta["keywords"] = $row['meta_keywords'];
    } else {
        $meta["title"] = $row['titolo_en'] . "|Kitchen school recipe";
        $meta["description"] = $row['meta_description_en'];
        $meta["keywords"] = $row['meta_keywords_en'];
        $prefix = "/en/";
        $labelRecipe = "recipe";
    }
    $myTemplate = new Template_Manager("ricetta", null, true, 7, $meta);
    $myTemplate->setBodyContentString("id", $row['id']);
    if (getLanguage() == 'it') {
        $myTemplate->setBodyContentString("titolo", $row['titolo']);
        $myTemplate->setBodyContentString("descr", $row['descrizione']);
        $myTemplate->setBodyContentString("dosi", $row['ingredienti']);
    } else {
        $myTemplate->setBodyContentString("titolo", $row['titolo_en']);
        $myTemplate->setBodyContentString("descr", $row['descrizione_en']);
        $myTemplate->setBodyContentString("dosi", $row['ingredienti_en']);
    }
    
    $myTemplate->setBodyContentString("tempo", $row['tempo']);
    $myTemplate->setBodyContentString("autore", $row['autore']);
    $myTemplate->setBodyContentImage("immagine", $row['uri']);
    $difficolta = "";
    for ($i = 0; $i < $row['difficolta']; $i++) {
        $difficolta = $difficolta . '<i class="icon-star"></i>';
    }
    $myTemplate->setBodyContentString("difficolta", $difficolta);
    $myTemplate->setBodyContentString("persone", $row['persone']);
    
    
    $myTemplate->setCanonicalUrl($prefix . $labelRecipe . "/" . $_REQUEST['titolo'] . "/");

    $myTemplate->setBodyContentString("nav_message", L::nav_message);
    $myTemplate->setBodyContentString("nav_home", L::nav_home);
    $myTemplate->setBodyContentString("nav_page", L::messages_recipe_nav_page);
    $myTemplate->setBodyContentString("author", L::messages_recipe_author);
    $myTemplate->setBodyContentString("people", L::messages_recipe_people);
    $myTemplate->setBodyContentString("difficulty", L::messages_recipe_difficulty);
    $myTemplate->setBodyContentString("time", L::messages_recipe_time);
    $myTemplate->setBodyContentString("doses", L::messages_recipe_doses);
    $myTemplate->setBodyContentString("description", L::messages_recipe_description);
}

